﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC27BO
    /// </summary>
    public class VYC27BO:DBConnectionBO
    {
        public VYC27BO():base()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public List<ModelVYC27> GetAllList()
        {
            var list = new List<ModelVYC27>();
            const string query = "SELECT [Id],[UniqueId],[Ten],[QuocKy],[GioiThieu],[TungDo],[ViDo],[DonViTienTe],[NgonNgu],[ThuDo],[NgayCapNhat],[TrangThai] FROM [VYC-27]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC27();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.UniqueId = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.Ten = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.QuocKy = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.GioiThieu = (dataTable.Rows[i][4] == DBNull.Value) ? string.Empty : dataTable.Rows[i][4].ToString();
                model.TungDo = (dataTable.Rows[i][5] == DBNull.Value) ? string.Empty : dataTable.Rows[i][5].ToString();
                model.ViDo = (dataTable.Rows[i][6] == DBNull.Value) ? string.Empty : dataTable.Rows[i][6].ToString();
                model.DonViTienTe = (dataTable.Rows[i][7] == DBNull.Value) ? string.Empty : dataTable.Rows[i][7].ToString();
                model.NgonNgu = (dataTable.Rows[i][8] == DBNull.Value) ? string.Empty : dataTable.Rows[i][8].ToString();
                model.ThuDo = (dataTable.Rows[i][9] == DBNull.Value) ? string.Empty : dataTable.Rows[i][9].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][10] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][10];
                model.TrangThai = (dataTable.Rows[i][11] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][11]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC27 model)
        {
            var query = "INSERT INTO [VYC-27]([UniqueId],[Ten],[QuocKy],[GioiThieu],[TungDo],[ViDo],[DonViTienTe],[NgonNgu],[ThuDo],[NgayCapNhat],[TrangThai]) VALUES ('" + model.UniqueId + "', N'" + model.Ten + "', N'" + model.QuocKy + "', N'"
                        + model.GioiThieu + "', '" + model.TungDo + "', '" + model.ViDo + "', N'" + model.DonViTienTe + "', N'" +
                        model.NgonNgu + "', N'" + model.ThuDo + "', " +
                        "GETDATE(), " + model.TrangThai + ")";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC27 model)
        {
            var query = "UPDATE [VYC-27] SET " +
                        "[UniqueId] = '" + model.UniqueId + "', " +
                        "[Ten]= N'" + model.Ten + "', " +
                        "[QuocKy] = N'" + model.QuocKy + "', " +
                        "[GioiThieu] = N'" + model.GioiThieu + "', " +
                        "[TungDo] = '" + model.TungDo + "', " +
                        "[ViDo]= '" + model.ViDo + "', " +
                        "[DonViTienTe] = N'" + model.DonViTienTe + "', " +
                        "[NgonNgu] = N'" + model.NgonNgu + "', " +
                        "[ThuDo] = N'" + model.ThuDo + "', " +
                        "[NgayCapNhat] = GETDATE(), [TrangThai] = " + model.TrangThai +
                        " WHERE Id =" + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC27 model)
        {
            var query = "DELETE FROM [VYC-27] " +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC27> GetById(int id)
        {
            var list = new List<ModelVYC27>();
            var query = "SELECT [Id],[UniqueId],[Ten],[QuocKy],[GioiThieu],[TungDo],[ViDo],[DonViTienTe],[NgonNgu],[ThuDo],[NgayCapNhat],[TrangThai] FROM [VYC-27] WHERE Id = " + id;
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC27();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.UniqueId = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.Ten = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.QuocKy = (dataTable.Rows[i][3] == DBNull.Value) ? string.Empty : dataTable.Rows[i][3].ToString();
                model.GioiThieu = (dataTable.Rows[i][4] == DBNull.Value) ? string.Empty : dataTable.Rows[i][4].ToString();
                model.TungDo = (dataTable.Rows[i][5] == DBNull.Value) ? string.Empty : dataTable.Rows[i][5].ToString();
                model.ViDo = (dataTable.Rows[i][6] == DBNull.Value) ? string.Empty : dataTable.Rows[i][6].ToString();
                model.DonViTienTe = (dataTable.Rows[i][7] == DBNull.Value) ? string.Empty : dataTable.Rows[i][7].ToString();
                model.NgonNgu = (dataTable.Rows[i][8] == DBNull.Value) ? string.Empty : dataTable.Rows[i][8].ToString();
                model.ThuDo = (dataTable.Rows[i][9] == DBNull.Value) ? string.Empty : dataTable.Rows[i][9].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][10] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][10];
                model.TrangThai = (dataTable.Rows[i][11] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][11]);

                list.Add(model);
            }
            return list;
        }
    }
}